<?php
namespace Admin\Controller;
use Think\Controller;
class userController extends Controller {

    //显示用户列表方法
    public function userlistJson(){
       
       $use= $this->users=M('user')->select();
   echo json_encode($use);
      } 
  
    public function userList() {
        $userModel = M('user');
        $userList = $userModel -> select();

        $this -> assign('userList',$userList);
        $this -> display();
    }
   public function doAdduser() {
          $userModel = M('user');
        //上传图片
        if ($_FILES['userpic']['error'] == 0) {
            $imageInfo = $this->upload();
            $pname = $imageInfo['userpic']['savename'];
        } else {
            $pname = "default.jpg";
        }
        $adduserData['userid'] = NULL;
        $adduserData['username'] = I('post.username');
        $adduserData['userpassword'] = I('post.userpassword');
        $adduserData['usertel'] = I('post.usertel');


        $res = $userModel -> add($adduserData);

        if ($res) {
            $this -> success("添加成功!");
        } else {
            $this -> error("添加失败!");
        }
    }
    public function updateuser($id){
        $userModel = M('user');
        $userData['userid'] = $id;
        $res = $userModel -> where($userData) -> find();
        $this -> assign('userInfo', $res);
        $this -> display();
    }
    public function doupdateuser() {
        $userModel = M('user');

        $updateuserData['userid'] = I('post.userid');
        $updateuserData['username'] = I('post.username');
        $updateuserData['userpassword'] = I('post.userpassword');
        $updateuserData['usertel'] = I('post.usertel');

        $res = $userModel -> save($updateuserData);

        if ($res) {
            $this -> success("修改成功!",U('user/userList'));
        } else {
            $this -> error("修改失败!");
        }
    }
       public function deluser($id){
       $res= M("user") -> where("userid = '$id'") -> delete();
       if ($res) {
            $this -> success("删除成功!",U('user/userList'));
        } else {
            $this -> error("修改失败!");
        }
    }

    

    //  public function index(){
    //     $share = M('share');
    //     //用户名条件搜索
    //     if(!empty($_GET['name'])){
    //       $where['name'] = array('like','%'.$_GET['name'].'%');
    //     }
    //     $total=$user->where($where)->count();
        
    //     //实例化分页类
    //     $page=new \Think\Page($total,5);
    //     $this->assign('show',$page->show());
    //     $data=$user->where($where)->page(I('get.p',1),5)->select();
    //     $this->assign('data',$data);
    //     $this->display();
    // }
public function Upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/user/'; // 设置附件上传根目录
      //  $upload->savePath  =   'Banner/'; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $info = $upload->upload(); // 上传文件

        if (!$info) {
            $this->error($upload->getError());
        } else {
            return $info;
        }
    }
}
